package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntKwlqdBaDetailBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntKwlqdBaDetailVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntKwlqdBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntKwlqdBaDetailMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntKwlqdBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 水泥混凝土抗弯拉强度试验检测记录详情Service业务层处理
 *
 * @author zxs
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntKwlqdBaDetailServiceImpl implements IReportSnhntKwlqdBaDetailService {

    private final ReportSnhntKwlqdBaDetailMapper baseMapper;

    /**
     * 查询水泥混凝土抗弯拉强度试验检测记录详情
     */
    @Override
    public ReportSnhntKwlqdBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询水泥混凝土抗弯拉强度试验检测记录详情列表
     */
    @Override
    public TableDataInfo<ReportSnhntKwlqdBaDetailVo> queryPageList(ReportSnhntKwlqdBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntKwlqdBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntKwlqdBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水泥混凝土抗弯拉强度试验检测记录详情列表
     */
    @Override
    public List<ReportSnhntKwlqdBaDetailVo> queryList(ReportSnhntKwlqdBaDetailBo bo) {
        LambdaQueryWrapper<ReportSnhntKwlqdBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntKwlqdBaDetail> buildQueryWrapper(ReportSnhntKwlqdBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntKwlqdBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportSnhntKwlqdBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getShijbh()), ReportSnhntKwlqdBaDetail::getShijbh, bo.getShijbh());
        lqw.eq(bo.getZhizrq() != null, ReportSnhntKwlqdBaDetail::getZhizrq, bo.getZhizrq());
        lqw.eq(bo.getShiyrq() != null, ReportSnhntKwlqdBaDetail::getShiyrq, bo.getShiyrq());
        lqw.eq(StringUtils.isNotBlank(bo.getLingq()), ReportSnhntKwlqdBaDetail::getLingq, bo.getLingq());
        lqw.eq(StringUtils.isNotBlank(bo.getShijcc()), ReportSnhntKwlqdBaDetail::getShijcc, bo.getShijcc());
        lqw.eq(bo.getPohhz() != null, ReportSnhntKwlqdBaDetail::getPohhz, bo.getPohhz());
        lqw.eq(bo.getKangwlqddz() != null, ReportSnhntKwlqdBaDetail::getKangwlqddz, bo.getKangwlqddz());
        lqw.eq(bo.getKangwlqdpj() != null, ReportSnhntKwlqdBaDetail::getKangwlqdpj, bo.getKangwlqdpj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhansjqdbfb()), ReportSnhntKwlqdBaDetail::getZhansjqdbfb, bo.getZhansjqdbfb());
        return lqw;
    }

    /**
     * 新增水泥混凝土抗弯拉强度试验检测记录详情
     */
    @Override
    public Boolean insertByBo(ReportSnhntKwlqdBaDetailBo bo) {
        ReportSnhntKwlqdBaDetail add = BeanUtil.toBean(bo, ReportSnhntKwlqdBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改水泥混凝土抗弯拉强度试验检测记录详情
     */
    @Override
    public Boolean updateByBo(ReportSnhntKwlqdBaDetailBo bo) {
        ReportSnhntKwlqdBaDetail update = BeanUtil.toBean(bo, ReportSnhntKwlqdBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntKwlqdBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水泥混凝土抗弯拉强度试验检测记录详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
